import { Component, OnInit } from '@angular/core';
import { SubmissionsService} from './submissions.service';
import { Location } from '@angular/common';
import { Response} from '@angular/http';
import {GlobalService} from "../../global.service";
import {User} from "../../user/model/user-model";
import {ProblemRelatedService} from "../problem-related.service";

@Component({
  selector: 'app-submissions',
  templateUrl: './submissions.component.html',
  styleUrls: ['./submissions.component.css']
})
export class SubmissionsComponent implements OnInit {

  code:string;
  problemInfos:ProblemInfo[] = [];
  problemCodes:string;
  problemCode:string;
  creator:string;
  judgeInfos:JudgeInfo[] = [];
  pageNumber:number;
  total:number;
  user:User;
  problemSetName:any;

  constructor(
    private globalService:GlobalService,
    private submissionsService:SubmissionsService,
    private location:Location,
    private problemRelatedService:ProblemRelatedService,
  ) { }

  ngOnInit() {

    let url = this.location.path();
    this.code = url.substring(url.indexOf("problem-sets/")+13,url.lastIndexOf("/"));


    this.user = this.globalService.getCurrentUser();

    this.problemCode = "";
    this.creator = "";
    this.pageNumber=0;
    this.total = 0;

    this.submissionsService.findProblemSet({
      code:this.code
    }).subscribe((res:Response)=>{
      this.problemCodes = '';

      for(let i = 0;i<res.json().value.problems.length;i++){
        let item = res.json().value.problems[i];
        this.problemInfos.push({
          code:item.code,
          name:item.name
        });
        this.problemCodes +=item.code+",";
      }
      if(this.problemCodes.length>0){
        this.problemCodes = this.problemCodes.substring(0,this.problemCodes.length-1);
      }else{
        this.problemCodes = "@Bing";//无效的code
      }
      this.search(null);
    });

    //查找题目集信息
    let problemSetInfo = {code:this.code};
    this.problemRelatedService.getProblemsBySetsCode(problemSetInfo)
      .subscribe((res:Response)=>{
        let response = res.json();
        if (response.success){
          console.log("题目集信息:",response.value);
          this.problemSetName = response.value.name;
        }
      });

  }

  search(event){

    if(event!=null){
      this.pageNumber=0;
    }

    let queryData = {
      properties:new Properties
    };
    queryData.properties.creator = this.creator;
    queryData.properties.pageNumber = this.pageNumber;
    queryData.properties.pageSize = 10;
    if(this.problemCode.length>0){
      queryData.properties.problemCode=this.problemCode;
    }else{
        queryData.properties.problemCode_in = this.problemCodes;
    }
    console.log("queryData",queryData);
    this.submissionsService.findPageJudgeInfos(queryData).subscribe((res:Response)=>{
      this.judgeInfos = [];
      this.total = res.json().total;

      for(let i =0;i<res.json().rows.length;i++){
        let item = res.json().rows[i];
        this.judgeInfos.push({
          createTime:item.createTime,
          statusName:item.statusName,
          score:item.score,
          name:item.name,
          compile:item.compile,
          runTime:item.runTime,
          creator:item.creator,
        });
      }

      res.json().rows.forEach(function (item) {

      });
    });
  }

  paginate(event) {
    this.pageNumber = event.page;
    this.search(null);
  }

}
class ProblemInfo{
  code:string;
  name:string;
}
class JudgeInfo{
  createTime:string;
  statusName:string;
  score:number;
  name:string;
  compile:string;
  runTime:any;
  creator:string
}
class Properties{
  creator:string;
  pageNumber:number;
  pageSize:number;
  problemCode:string;
  problemCode_in:string;
}
